//
//  MusicBankViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "MusicBankViewController.h"
#import "RadioCollectionViewCell.h"
#import "SongMenuCollectionViewCell.h"
#import "SongerCollectionViewCell.h"
#import "SeniorityCollectionViewCell.h"
#import "MVCollectionViewCell.h"
#import "SongerViewController.h"
#import "CXYSongsMenuViewController.h"
#import "HotListenListViewController.h"

@interface MusicBankViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SliderViewDelegate, SongerCollectionViewCellDelegate,
    SongMenuCollectionView, SeniorityCollectionViewCellDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) SliderView *sliderView;
@end

@implementation MusicBankViewController

- (void)dealloc
{
    [_collection release];
    [_array release];
    [_sliderView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建collection
    [self buildCollection];
}

- (void)buildCollection {
    
    self.array = [NSMutableArray arrayWithObjects:@"排行", @"歌单", @"电台", @"歌手", nil];
    self.sliderView = [[SliderView alloc] initWithFrame:CGRectMake(0, 0, 375, 30) andNSArray:_array];
    self.sliderView.delegate = self;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, 375, 667 - 150) collectionViewLayout:flowLayout];
    self.collection.showsHorizontalScrollIndicator = NO;
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"backGroud" ofType:@"jpg"]]];
    
    
    self.collection.pagingEnabled = YES;
    [self.collection registerClass:[RadioCollectionViewCell class] forCellWithReuseIdentifier:@"radioCell"];
    [self.collection registerClass:[MVCollectionViewCell class] forCellWithReuseIdentifier:@"MvCell"];
    [self.collection registerClass:[SongerCollectionViewCell class] forCellWithReuseIdentifier:@"songerCell"];
    [self.collection registerClass:[SongMenuCollectionViewCell class] forCellWithReuseIdentifier:@"songMenuCell"];
    [self.collection registerClass:[SeniorityCollectionViewCell class] forCellWithReuseIdentifier:@"seniorityCell"];
    
    [self.view addSubview:_sliderView];
    [self.view addSubview:_collection];
    [_collection release];
    [_sliderView release];
}

//代理事件
- (void)changeCellWithButton:(NSInteger)num
{
    NSInteger current = self.collection.contentOffset.x / self.view.frame.size.width;
    NSInteger n = current - num;
    if (labs(n) > 1)
    {
        [self.collection setContentOffset:CGPointMake(num * [UIScreen mainScreen].bounds.size.width, 0) animated:NO];
    } else {
        [self.collection setContentOffset:CGPointMake(num * [UIScreen mainScreen].bounds.size.width, 0) animated:YES];
    }
}

//全部歌单模态
- (void)CXYPushButton:(UIButton *)sender
{
    CXYSongsMenuViewController *cxySongsMenuViewController = [[CXYSongsMenuViewController alloc] init];
    [self presentViewController:cxySongsMenuViewController animated:YES completion:^{
    }];
}

//跳转歌单详情页
- (void)CXYPushSongsMenuController:(NSString *)idString
{
    HotListenListViewController *hot = [[HotListenListViewController alloc] init];
    hot.idString = idString;
    hot.numberString = @"http://api.songlist.ttpod.com/songlists/";
    [self.navigationController pushViewController:hot animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [hot release];
}
//滑动偏移事件
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger num = self.collection.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
    
    //绿线移动
    CGRect rect = self.sliderView.imageViewLine.frame;
    rect.origin.x = num * 90 + 15;
    [UIView animateWithDuration:0.3f animations:^{
        self.sliderView.imageViewLine.frame = rect;
    }];
    for (NSInteger i = 0; i < 4; i++) {
        if (num == i) {
            [(UIButton *)[self.sliderView.scroller viewWithTag:i + 1000] setTitleColor:[UIColor colorWithRed:61 / 255.0 green:145 / 255.0 blue:65 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        }else{
        [(UIButton *)[self.sliderView.scroller viewWithTag:i + 1000] setTitleColor:[UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        }
    }
    
}
//返回单元个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

//返回分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row)
    {
        SeniorityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"seniorityCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    } else if (1 == indexPath.row) {
        SongMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"songMenuCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    } else if (2 == indexPath.row) {
        RadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"radioCell" forIndexPath:indexPath];
        return cell;
    } else {
        SongerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"songerCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
}



- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(375, 667 - 150);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//排行详情界面
- (void)pushHot:(NSString *)string
{
    HotListenListViewController *hot = [[HotListenListViewController alloc] init];
    hot.idString = [NSString stringWithFormat:@"%@/current", string];
    hot.numberString = @"http://api.songlist.ttpod.com/ranklists/";
    [self.navigationController pushViewController:hot animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [hot release];
}


//跳到歌手页面
- (void)pushSongerController:(SongerModel *)songerModel
{
    SongerViewController *songerViewController = [[[SongerViewController alloc] init] autorelease];
    songerViewController.songerModel = songerModel;
    [self.navigationController pushViewController:songerViewController animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
